//
//  WmcManualAddTeamMemberAlert.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/10/8.
//

import UIKit
import YPUIKit

class WmcManualAddTeamMemberAlert: WmcBaseView {
    private var confirmB: (() -> Void)?

    class func alertView(title: String? = "",
                         content: String? = "",
                         btnTitle: String? = "确定",
                         confirmB: (() -> Void)? = nil) -> WmcManualAddTeamMemberAlert
    {
        let alert = WmcManualAddTeamMemberAlert()
        alert.titleLabel.text = title
        alert.contentLabel.text = content
        alert.confirmButton.setTitle(btnTitle, for: .normal)
        alert.confirmB = confirmB
        return alert
    }

    override func makeUI() {
        layer.cornerRadius = 8
        clipsToBounds = true

        addSubview(titleLabel)
        addSubview(contentLabel)
        addSubview(hLineView)
        addSubview(confirmButton)

        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(24)
            make.left.equalTo(16)
            make.right.equalTo(-16)
        }

        contentLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
            make.left.equalTo(16)
            make.right.equalTo(-16)
        }

        hLineView.snp.makeConstraints { make in
            make.top.equalTo(contentLabel.snp.bottom).offset(24)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }

        confirmButton.snp.makeConstraints { make in
            make.top.equalTo(hLineView.snp.bottom).offset(12)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-12)
        }

        confirmButton.rx.controlEvent(.touchUpInside).bind { [weak self] _ in
            guard let weakSelf = self, let block = weakSelf.confirmB else { return }
            weakSelf.dismiss()
            block()
        }.disposed(by: rx.disposeBag)
    }

    lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.font = 18.yp.uiFont(weight: .semibold)
        view.textColor = UIColor.wmc_grayH2_323233
        view.textAlignment = .center
        view.numberOfLines = 0
        return view
    }()

    lazy var contentLabel: UILabel = {
        let view = UILabel()
        view.font = 16.yp.uiFont
        view.textColor = UIColor.wmc_grayH8_8A8A99
        view.textAlignment = .center
        view.numberOfLines = 0
        return view
    }()

    lazy var hLineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.wmc_grayH19_F5F5F5
        return view
    }()

    lazy var confirmButton: UIButton = {
        let view = UIButton()
        view.setTitleColor(UIColor.wmc_mainTheme, for: .normal)
        view.titleLabel?.font = 18.yp.uiFont
        return view
    }()
}

extension WmcManualAddTeamMemberAlert: YPUIPresentTarget {
    func show() {
        let config = YPUIPresentController.Config()
        config.contentView = self
        config.isTouchMaskDismiss = false
        config.transitionAnimated.usingSpringWithDamping = .init(appearDuration: 1, disappearDuration: 1)
        config.transitionAnimated.initialSpringVelocity = .init(appearDuration: 0, disappearDuration: 0)
        config.style = .alert(.Create { make in
            make.center.equalToSuperview()
            make.left.equalTo(32)
            make.right.equalTo(-32)
        })
        present(config: config)
    }
}
